#! perl -w

sub on_start {
    my ($self) = @_;
    $self->grab_button (3, urxvt::ControlMask);
    ()
}

sub on_button_press {
    my ($self, $event) = @_;

    if ($event->{button} == 3) {
        my $popup = $self->popup ($event)
            or return 1;

        my ($word) = $self->selection =~ m/([a-zA-Z]+)/;

        open PIPE, "export LANG=zh_CN.UTF-8; /usr/bin/sdcv -n --utf8-output -u 'XDICT英汉辞典' '$word' | tail -n +5 | head -15 | head -n -1 |"
            or close PIPE;

        for my $eachline (<PIPE>) {
            chomp $eachline;
            $eachline = $self->locale_decode($eachline);
            $popup->add_title ($eachline);
        }
        close PIPE;

        $popup->show;
    }
    ()
}
